<?php

include_once ('bn_path.php');

class bn_template
{
    public $tempath = '';
    public $temfile = 'user_theme.php';
    private $data = array();
    private $meta = '';
    private $link = '';
    private $body = '';
    private $script = '';
    private $style = '';
    private $current;
    protected static $instance;


    public static function getInstance()
    {
        if (!isset(self::$instance))
        {
            self::$instance = new self();
        }
        return self::$instance;
    }

    public function __get($name)
    {
        return $this->data[$name];
    }

    public function __set($name, $value)
    {
        if (isset($this->data[$name]))
            $this->data[$name] .= "\n$value";
        else
            $this->data[$name] = $value;
    }

    public function add_block($name)
    {
        if (empty($this->current))
        {
            ob_clean();
            $this->current = $name;
        } else
        {
            if (isset($this->data[$this->current]))
                $this->data[$this->current] .= "\n" . ob_get_clean();
            else
                $this->data[$this->current] = ob_get_clean();
            $this->current = $name;
            ob_start();
        }
    }

    public function add_meta($metaname, $metaval)
    {
        $this->meta .= " $metaname='$metaval'";
    }

    public function add_link($filepath)
    {
        $ext = pathinfo($filepath, PATHINFO_EXTENSION);
        if (strtolower($ext) == 'css')
            $this->link .= "\n <link rel='stylesheet' type='text/css' href='$filepath'>";
        if (strtolower($ext) == 'js')
            $this->link .= "\n<script type='text/javascript' src='$filepath'></script>";
    }

    public function add_body($body)
    {
        $this->body .= "\n$body";
    }


    public function add_script($script)
    {
        $this->script .= "\n$script";
    }

    public function add_style($style)
    {
        $this->style .= "\n$style";
    }


    private function search_pattern($text, $pattern)
    {
        $pos = strpos($text, $pattern);
        if ($pos != false)
            $pos +=strlen($pattern);
        return $pos;
    }

     private function TextBetweenTags($text,$pattern)
    {
        $pos = strpos($text, "<!--$pattern-->");
        $pos2=strpos($text, "<!--/$pattern-->");
        if ($pos != false&&$pos2!=false)
        {
            $pos +=strlen($pattern)+7;
            $pos2-=$pos;
            return substr($text,$pos,$pos2); 
        } 
    }

    public function render($function = null)
    {
        if (empty($this->current))
            $this->body .= "\n" . ob_get_clean();
        else
        {
            if (!empty($this->data[$this->current]))
                $this->data[$this->current] .= "\n" . ob_get_clean();
            else
                $this->data[$this->current] = ob_get_clean();
        }

        if ($this->tempath == '')
        {
            $this->tempath = bn_get_dir_template();
        }
        ob_start($function);
        if (file_exists($this->tempath . "/$this->temfile"))
        {
            include_once ($this->tempath . "/$this->temfile");
            $theme = ob_get_clean();

            foreach ($this->data as $key => $value)
            {
                if (is_array($value))
                {
                    $str = self::TextBetweenTags($theme, $key);
                    $rep = '';
                    foreach ($value as $row)
                    {
                        $rw = $str;
                        if(is_object($row))
                        $row=(array)$row;
                        foreach ($row as $k => $v)
                        {
                            $rw = str_replace("<%$k%>", $v, $rw);
                        }
                        $rep .= $rw; 
                    }
                    $theme = str_replace("<!--$key-->$str<!--/$key-->", $rep, $theme);
                } else
                    $theme = str_replace("<!--$key-->", $value, $theme);
            }
            $bodypos = self::search_pattern($theme, '<body>');
            if ($this->body != '' && isset($bodypos))
            {
                $theme = substr_replace($theme, $this->body, $bodypos, 0);
            }

            $bodypos = self::search_pattern($theme, '</head>');
            if ($this->style != '' && isset($bodypos))
            {
                $this->style = "<style>\n$this->style\n</style>\n";
                $theme = substr_replace($theme, $this->style, $bodypos - 7, 0);
            }

            $bodypos = self::search_pattern($theme, '</body>');
            if ($this->script != '' && isset($bodypos))
            {
                $this->script = "<script>\n jQuery(document).ready(function (){\n$this->script\n});\n</script>\n";
                $theme = substr_replace($theme, $this->script, $bodypos - 7, 0);
            }


            $bodypos = self::search_pattern($theme, '<title>');
            if ($this->meta != '' && isset($bodypos))
            {
                $this->meta = "\n<meta " . $this->meta . " />\n";

                $theme = substr_replace($theme, $this->meta, $bodypos - 7, 0);
            }

            $bodypos = self::search_pattern($theme, '<style>');
            if ($bodypos === false)
                $bodypos = self::search_pattern($theme, '</head>');
            if ($this->link != '' && isset($bodypos))
            {
                $theme = substr_replace($theme, $this->link, $bodypos - 7, 0);
            }

        } else
            $theme = 'themplate file not found';

        die($theme);
    }
}

?>